#include "output.h"
#include "vertex.h"
#include "edge.h"

#include <QDebug>
#include <QTextStream>

Output::Output(QFile *_file) : file(_file){
}

Output::Output(){
}

Output::~Output(){
}

void Output::parseIn(Scene *_s){
	Object *object;
	Vertex *vertex;
	Point p;

	QTextStream stream(file);

	stream << _s->sizeObjects() << "\n";

	for(int i = 0; i < _s->sizeObjects(); i++){
	    object = _s->getObject(i);
        stream << object->sizeVertices() << "\n";
        for(int j = 0; j < object->sizeVertices(); j++){
            vertex = object->getVertex(j);
            p = vertex->getPoint();
            stream << vertex->getID() << " " << p.x << " " << p.y << " " << p.z << "\n";
        }
	}
}

void Output::parseIn(QFile *_file, Scene *_s) {
	file = _file;
	return parseIn(_s);
}

void Output::parseOut(Scene *_s){
	Object *object;
	Vertex *vertex;
	Edge *edge;
	bool edgeSignal;
	Face *face;
	Point p;

	QTextStream stream(file);

	stream << _s->sizeObjects() << "\n";

	for(int i = 0; i < _s->sizeObjects(); i++){
	    object = _s->getObject(i);
	    if(!object->haveConvexHull()){
	        qDebug() << "Problema ao salvar, objeto " << object->getID() << " não tem seu fecho convexo.";
	        return;
	    }
        stream << object->sizeVertices() << "\n";
        for(int j = 0; j < object->sizeVertices(); j++){
            vertex = object->getVertex(j);
            p = vertex->getPoint();
            stream << vertex->getID() << " " << p.x << " " << p.y << " " << p.z << "\n";
        }
        stream << object->sizeFaces() << "\n";
        for(int j = 0; j < object->sizeFaces(); j++){
            face = object->getFace(j);
            stream << face->getID();

            edge = face->getEdge();
            edgeSignal = face->getEdgeSignal();

            do{
                if(edgeSignal){
                    vertex = edge->getStart();
                    stream << " " << vertex->getID();

                    edgeSignal = edge->getNextClockwiseSignal();
                    edge = edge->getNextClockwise();

                }else{
                    vertex = edge->getEnd();
                    stream << " " << vertex->getID();

                    edgeSignal = edge->getNextCounterClockwiseSignal();
                    edge = edge->getNextCounterClockwise();
                }
            }while (edge != face->getEdge());
            if(j != object->sizeFaces() - 1) stream << "\n";
        }
	}
}

void Output::parseOut(QFile *_file, Scene *_s) {
	file = _file;
	return parseOut(_s);
}

void Output::parseWed(Scene *_s){
	int id, idE;
	bool signalE;
	Object *object;
	Face *face;
	Edge *edge, *edge2;
	Vertex *vertex;
	Point p;

	QTextStream stream(file);

	stream << _s->sizeObjects() << "\n";

	for(int i = 0; i < _s->sizeObjects(); i++){
	    object = _s->getObject(i);
	    if(!object->haveConvexHull()){
	        qDebug() << "Problema ao salvar, objeto " << object->getID() << " não tem seu fecho convexo.";
	        return;
	    }
        stream << object->sizeVertices() << "\n";
        for(int j = 0; j < object->sizeVertices(); j++){
            vertex = object->getVertex(j);
            id = vertex->getID();
            p = vertex->getPoint();
            edge = vertex->getEdge();
            if(edge){
                idE = edge->getID();
                signalE = vertex->getEdgeSignal();
                if(!signalE){
                    idE = -idE;
                }
                stream << id << " " << p.x << " " << p.y << " " << p.z << " " << idE << "\n";
            }else{
                stream << id << " " << p.x << " " << p.y << " " << p.z << " -\n";
            }
        }
        stream << object->sizeFaces() << "\n";
        for(int j = 0; j < object->sizeFaces(); j++){
            face = object->getFace(j);
            id = face->getID();
            edge = face->getEdge();           //se não existir essa edge, tem algo errado
            idE = edge->getID();
            signalE = face->getEdgeSignal();
            if(!signalE){
                idE = -idE;
            }
            stream << id << " " << idE << "\n";
        }
        stream << object->sizeEdges() << "\n";
        for(int j = 0; j < object->sizeEdges(); j++){
            edge = object->getEdge(j);
            id = edge->getID();
            stream << id << " ";

            vertex = edge->getStart();
            id = vertex->getID();
            stream << id << " ";

            vertex = edge->getEnd();
            id = vertex->getID();
            stream << id << " ";

            face = edge->getClockwise();
            id = face->getID();
            stream << id << " ";

            face = edge->getCounterClockwise();
            id = face->getID();
            stream << id << " ";

            edge2 = edge->getNextClockwise();
            idE = edge2->getID();
            signalE = edge->getNextClockwiseSignal();
            if(!signalE){
                idE = -idE;
            }
            stream << idE << " ";

            edge2 = edge->getPreviousClockwise();
            idE = edge2->getID();
            signalE = edge->getPreviousClockwiseSignal();
            if(!signalE){
                idE = -idE;
            }
            stream << idE << " ";

            edge2 = edge->getNextCounterClockwise();
            idE = edge2->getID();
            signalE = edge->getNextCounterClockwiseSignal();
            if(!signalE){
                idE = -idE;
            }
            stream << idE << " ";

            edge2 = edge->getPreviousCounterClockwise();
            idE = edge2->getID();
            signalE = edge->getPreviousCounterClockwiseSignal();
            if(!signalE){
                idE = -idE;
            }
            stream << idE << "\n";
        }
	}
}

void Output::parseWed(QFile *_file, Scene *_s) {
	file = _file;
	return parseWed(_s);
}

void Output::parseScene(Scene *_s){
	Object *object;
	Vertex *vertex;
	Point p;

	QTextStream stream(file);

	for(int i = 0; i < _s->sizeObjects(); i++){
	    object = _s->getObject(i);
        for(int j = 0; j < object->sizeVertices(); j++){
            vertex = object->getVertex(j);
            p = vertex->getPoint();
            stream << "v " << p.x << " " << p.y << " " << p.z << "\n";
        }
        if(i != _s->sizeObjects() - 1) stream << "-\n";
	}
}

void Output::parseScene(QFile *_file, Scene *_s) {
	file = _file;
	return parseScene(_s);
}

void Output::parseObject(Object *_o){
	Vertex *vertex;
	Point p;

	QTextStream stream(file);

    for(int j = 0; j < _o->sizeVertices(); j++){
        vertex = _o->getVertex(j);
        p = vertex->getPoint();
        stream << "v " << p.x << " " << p.y << " " << p.z << "\n";
    }
}

void Output::parseObject(QFile *_file, Object *_o){
	file = _file;
	return parseObject(_o);
}
